
package com.kanjian.music.dialog;

import android.app.Dialog;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;

import com.kanjian.music.R;
import com.kanjian.music.constants.IntentConstants;
import com.kanjian.music.entity.Music;
import com.kanjian.music.entity.Musician;
import com.kanjian.music.util.ThirdPartyShareUtil;

public class ShareDialog extends DialogFragment {

    private View mRootView;

    private int mShareType;

    private Music mMusic;

    private Musician mMusician;

    private String mRankName;

    private Button mBtn_sinaWeibo;

    private Button mBtn_tencentWeibo;

    private Button mBtn_mmFriend;

    private Button mBtn_mmCircle;

    private final int shareTo_sinaWeibo = 0;

    private final int shareTo_tencentWeibo = 1;

    private final int shareTo_mmFriend = 2;

    private final int shareTo_mmCircle = 3;

    public static final int SHARETYPE_SINGLEMUISIC = 0;

    public static final int SHARETYPE_MUSICIAN = 1;

    public static final int SHARETYPE_TOPLIST = 2;

    // 分享单曲
    public static ShareDialog newInstance(Music music) {
        ShareDialog dialog = new ShareDialog();
        Bundle args = new Bundle();
        args.putSerializable(IntentConstants.MUSIC, music);
        args.putInt(IntentConstants.SHARETYPE, SHARETYPE_SINGLEMUISIC);
        dialog.setArguments(args);
        return dialog;
    }

    // 分享音乐人
    public static ShareDialog newInstance(Musician musician) {
        ShareDialog dialog = new ShareDialog();
        Bundle args = new Bundle();
        args.putSerializable(IntentConstants.MUSICIAN, musician);
        args.putInt(IntentConstants.SHARETYPE, SHARETYPE_MUSICIAN);
        dialog.setArguments(args);
        return dialog;
    }

    // 分享榜单
    public static ShareDialog newInstance(String rankName) {
        ShareDialog dialog = new ShareDialog();
        Bundle args = new Bundle();
        args.putSerializable(IntentConstants.RANKNAME, rankName);
        args.putInt(IntentConstants.SHARETYPE, SHARETYPE_TOPLIST);
        dialog.setArguments(args);
        return dialog;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle b = getArguments();
        if (b != null) {
            mMusic = (Music) b.getSerializable(IntentConstants.MUSIC);
            mMusician = (Musician) b.getSerializable(IntentConstants.MUSICIAN);
            mRankName = b.getString(IntentConstants.RANKNAME);
            mShareType = b.getInt(IntentConstants.SHARETYPE);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        initView(inflater, container);
        return mRootView;
    }

    private void initView(LayoutInflater inflater, ViewGroup container) {
        if (mRootView == null) {
            mRootView = inflater.inflate(R.layout.dialog_share, null);
            mBtn_sinaWeibo = (Button) mRootView.findViewById(R.id.share_sina_weibo);
            mBtn_sinaWeibo.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    share(shareTo_sinaWeibo);
                    ShareDialog.this.dismissAllowingStateLoss();
                }
            });
            mBtn_tencentWeibo = (Button) mRootView.findViewById(R.id.share_tencent_weibo);
            mBtn_tencentWeibo.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    share(shareTo_tencentWeibo);
                    ShareDialog.this.dismissAllowingStateLoss();
                }
            });
            mBtn_mmFriend = (Button) mRootView.findViewById(R.id.share_mm_friend);
            mBtn_mmFriend.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    share(shareTo_mmFriend);
                    ShareDialog.this.dismissAllowingStateLoss();
                }
            });
            mBtn_mmCircle = (Button) mRootView.findViewById(R.id.share_mm_circle);
            mBtn_mmCircle.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    share(shareTo_mmCircle);
                    ShareDialog.this.dismissAllowingStateLoss();
                }
            });
        }
    }

    private void share(int shareTo) {
        switch (shareTo) {
            case shareTo_sinaWeibo:
                if (!findApp("com.sina.weibo")) {
                    return;
                }
                ThirdPartyShareUtil.ShareToWeibo(getActivity(), buildShareString(), "");
                break;
            case shareTo_tencentWeibo:
                if (!findApp("com.tencent.wblog")) {
                    return;
                }
                ThirdPartyShareUtil.ShareToTencentWeibo(getActivity(), buildShareString(), "");
                break;
            case shareTo_mmFriend:
                if (!findApp("com.tencent.mm")) {
                    return;
                }
                ThirdPartyShareUtil.ShareToMMFriend(getActivity(), buildShareString(), "");
                break;
            case shareTo_mmCircle:
                if (!findApp("com.tencent.mm")) {
                    return;
                }
                ThirdPartyShareUtil.ShareToMMCircle(getActivity(), buildShareString(), "");
                break;
        }
    }

    private boolean findApp(String packageName) {
        PackageInfo packageInfo;
        try {
            packageInfo = getActivity().getPackageManager().getPackageInfo(packageName, 0);
        } catch (NameNotFoundException e) {
            packageInfo = null;
            e.printStackTrace();
        }
        return packageInfo != null;
    }

    private String buildShareString() {
        switch (mShareType) {
            case SHARETYPE_SINGLEMUISIC:
                if (mMusic != null) {
                    return "分享了 " + mMusic.getUserName() + "的" + "#" + mMusic.getMusicName()
                            + "#\n" + "http://www.kanjian.com/song/" + mMusic.getMusicId();
                }
                break;
            case SHARETYPE_MUSICIAN:
                if (mMusician != null) {
                    return "分享了音乐人" + "#" + mMusician.mUserName + "#\n"
                            + "http://www.kanjian.com/user/" + mMusician.mUserId;
                }
                break;
            case SHARETYPE_TOPLIST:
                return "分享 @看见音乐 #" + mRankName + "#" + "，发现最酷的独立音乐\n" + "http://www.kanjian.com";
        }
        return "";
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        dialog.setCanceledOnTouchOutside(true);
        return dialog;
    }
}
